package com.mingjie.daydayup;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import util.utils;

public class CacheActivity extends AppCompatActivity {

    private String path;
    private File file;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cache);
        iv = (ImageView) findViewById(R.id.iv_view);
        path = "https://www.baidu.com/img/bd_logo1.png";
        file = new File(getCacheDir(), utils.getFileNameFromURL(path));
        if (file.exists()) {
            System.out.println("cache");
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            iv.setImageBitmap(bitmap);

        } else {
            try {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("url");
                        URL url = null;
                        try {
                            url = new URL(path);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            connection.setConnectTimeout(5000);
                            connection.setReadTimeout(5000);
                            if(connection.getResponseCode()==200){
                                InputStream in =connection.getInputStream();
                                FileOutputStream fileOutputStream = new FileOutputStream(file);
                                int len = 0;
                                byte[] bytes = new byte[1024];
                                while ((len = in.read(bytes)) != -1){
                                    fileOutputStream.write(bytes, 0, len);
                                }
                                fileOutputStream.close();
                                in.close();
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Bitmap bitmap=BitmapFactory.decodeFile(file.getAbsolutePath());
                                    iv.setImageBitmap(bitmap);
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }).start();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
